package com.easyUploader.controller;
import com.common.result.ResponseResultAdvice;
import com.easyUploader.property.LocalFileProperty;
import com.easyUploader.service.IBucketService;
import com.easyUploader.service.IFileStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.easyUploader.property.LocalFileProperty.DEFAULT_VISIT_CONTROLLER_PREFIX;

/**
 * @author 大忽悠
 * @create 2022/9/15 11:09
 */
@Validated
@RestController
@RequestMapping(DEFAULT_VISIT_CONTROLLER_PREFIX)
@RequiredArgsConstructor
@ResponseResultAdvice
@Api(tags = "文件访问")
public class FileVisitorController {
    private final IFileStorageService fileStorageService;

    /**
     * 访问格式: localhost:5200/uploader/{bucket}/{path} <br>
     * path=fileKey/filename <br>
     * 该接口提供在线访问功能,因此不支持通过目录名访问文件<br>
     * 只支持以下类型文件的在线访问: <br>
     * text/html ：HTML格式 <br>
     *
     * text/plain ：纯文本格式 <br>
     *
     * text/xml ： XML格式<br>
     *
     * image/gif ：gif图片格式<br>
     *
     * image/jpeg ：jpg图片格式<br>
     *
     * image/png：png图片格式<br>
     *
     * application/pdf ：pdf格式<br>
     *
     * 其他类型文件通通采用纯文本格式显示
     */
    @ApiOperation("根据桶和文件名访问文件")
    @GetMapping("/**")
    public void visitByBucketAndFileName(HttpServletRequest request, HttpServletResponse response) throws IOException {
        fileStorageService.visitByBucketAndFileName(request, response);
    }
}
